package com.guojian.sso;

import java.util.Collections;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import com.guojian.sso.filter.AuthFilter;
import com.guojian.sso.filter.SessionFilter;
import com.guojian.sso.session.SessionTomcatServletWebServerFactory;

/**
 * 
 * @SpringBootServletInitializer 实现了 @WebApplicationInitializer 的初始化依赖于
 *                               servlet3.0+
 * 
 * @HandlesTypes(WebApplicationInitializer.class) @SpringServletContainerInitializer
 *                                                implements @ServletContainerInitializer
 *                                                以war包的形式部署到Servlet_3.0+容器,会扫描所有jar包的META-INF/services/javax.servlet.ServletContainerInitializer文件,并读取文件中的class类加载
 *                                                总结:
 *                                                war包形式启动会调用 @WebApplicationInitializer.class
 *                                                的onStart方法
 * 
 * @author GuoJian
 */

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	// private static final Log log = LogFactory.getLog(Application.class);

	public static void main(String[] args) {
		SpringApplicationBuilder builder = new SpringApplicationBuilder();
		SpringApplication application = builder.build();
		application.addPrimarySources(Collections.singleton(Application.class));
		application.run();
	}

	public FilterRegistrationBean<AuthFilter> AuthFilterRegistration() {
		FilterRegistrationBean<AuthFilter> registration = new FilterRegistrationBean<>(new AuthFilter());
		registration.setOrder(0);
		// registration.addUrlPatterns(urlPatterns);
		return registration;
	}

	public FilterRegistrationBean<SessionFilter> sessionFilterRegistration() {
		FilterRegistrationBean<SessionFilter> registration = new FilterRegistrationBean<>(new SessionFilter());
		registration.setOrder(-111);
		return registration;
	}

	/**
	 * 
	 * @TomcatServletWebServerFactory webServer工厂方法在getWebServer的过程中,对server的connector和context进行配置,最后
	 *                                提供了protected void
	 *                                postProcessContext(Context context)
	 *                                方法,这样可以利于我们后续对context的拓展.
	 *                                替换spring提供的TomcatServletWebServerFactory
	 * @return
	 */
	@Bean
	public SessionTomcatServletWebServerFactory sessionTomcatServletWebServerFactory() {
		return new SessionTomcatServletWebServerFactory();
	}

}
